//
//  MessageViewController.m
//  jinwang
//
//  Created by jzb on 2018/2/24.
//  Copyright © 2018年 jzb. All rights reserved.
//

#import "MessageViewController.h"
#import "AddFriendsVC.h"
#import "JWMessageGoodFriendVC.h"

@interface MessageViewController ()<CLLocationManagerDelegate>
{
    CLLocationManager *_locationManager;//定位
}
//商铺
@property (nonatomic,strong) UIButton *storeBtn;
//添加view
@property (nonatomic,strong) UIView *storeBtnView;
//添加
@property (nonatomic,strong) UIButton *addBtn;
//搜索
@property (nonatomic,strong) UIButton *fangdajingBtn;

@property (nonatomic, strong) MLMSegmentHead *segHead;
@property (nonatomic, strong) MLMSegmentScroll *segScroll;

@end

@implementation MessageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    // app从后台进入前台都会调用这个方法
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updataJSONRequest) name:UIApplicationWillEnterForegroundNotification object:nil];
    [self loginHuanxin];
//    [self updataJSONRequest];
    _storeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _storeBtn.frame = CGRectMake(0, 10, 55, 26);
    _storeBtn.backgroundColor = kRGBColor(225, 74, 33);
    //        [_storeBtn setImage:[UIImage imageNamed:@"home_QRcode"] forState:UIControlStateNormal];
    [_storeBtn setTitle:@"商铺" forState:UIControlStateNormal];
    _storeBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [_storeBtn setTintColor:[UIColor whiteColor]];
    _storeBtn.layer.masksToBounds = YES;
    _storeBtn.layer.cornerRadius = 2;
    [_storeBtn addTarget:self action:@selector(storeBtnACTION:) forControlEvents:UIControlEventTouchUpInside];
    
    _storeBtnView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 55, 44)];
    [_storeBtnView addSubview:_storeBtn];
    
    _storeBtnView.hidden = YES;
    UIBarButtonItem *storeItem = [[UIBarButtonItem alloc]initWithCustomView:_storeBtnView];
    UIBarButtonItem *addItem = [[UIBarButtonItem alloc]initWithCustomView:self.addBtn];
    UIBarButtonItem *fangdajingItem = [[UIBarButtonItem alloc]initWithCustomView:self.fangdajingBtn];
    self.navigationItem.rightBarButtonItems = @[addItem,storeItem];
    self.navigationItem.leftBarButtonItem = fangdajingItem;
    
    [self initializeLocationService];
    
    NSArray *list = @[@"好友",@"商家",@"行人",@"群聊"];
    NSMutableArray *buttonImgArr = [NSMutableArray arrayWithArray:@[@"jw_message_haoyou",@"jw_message_shangjia",@"jw_message_xingren",@"jw_message_qunliao"]];
    NSMutableArray *buttonSelImgArr = [NSMutableArray arrayWithArray:@[@"jw_message_haoyou_sel",@"jw_message_shangjia_sel",@"jw_message_xingren_sel",@"jw_message_qunliao_sel"]];
    _segHead = [[MLMSegmentHead alloc] initWithFrame:CGRectMake(0, 64, kScreenWidth, 50) titles:list headStyle:SegmentHeadStyleDefault layoutStyle:MLMSegmentLayoutDefault];
    _segHead.buttonImgArray = buttonImgArr;
    _segHead.buttonSelImgArray = buttonSelImgArr;
    //    _segHead.fontScale = 1.1;
    _segHead.fontSize = 14;
    _segHead.deSelectColor = kRGBColor(80, 80, 80);
    _segHead.selectColor = UIColorHex(0xEEA72B);
    _segHead.lineScale = .4;
    _segHead.bottomLineColor = kRGBColor(230, 230, 230);
    
    _segScroll = [[MLMSegmentScroll alloc] initWithFrame:CGRectMake(0, 64+50, kScreenWidth, kScreenHeight-64-49-50) vcOrViews:[self vcArr:list.count]];
    
    _segScroll.loadAll = YES;
    _segScroll.showIndex = 0;
    
    //    _segScroll.backgroundColor = [UIColor purpleColor];
    
    [MLMSegmentManager associateHead:_segHead withScroll:_segScroll completion:^{
        
        [self.view addSubview:_segHead];
        [self.view addSubview:_segScroll];
    }];
    
}

- (void)initializeLocationService {
    
    // 初始化定位管理器
    _locationManager = [[CLLocationManager alloc] init];
    // 设置代理
    _locationManager.delegate = self;
    // 设置定位精确度到米
    _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    // 设置过滤器为无
    _locationManager.distanceFilter = kCLDistanceFilterNone;
    // 开始定位
    [_locationManager startUpdatingLocation];
}

#pragma mark - 数据源
- (NSArray *)vcArr:(NSInteger)count {
    NSMutableArray *arr = [NSMutableArray array];
    
    JWMessageGoodFriendVC *GoodFriend_VC = [[JWMessageGoodFriendVC alloc]init];
    UIViewController *Loveshot_VC = [[UIViewController alloc]init];
    UIViewController *GoodArticle_VC = [[UIViewController alloc]init];
    UIViewController *GoodArticle_VC0 = [[UIViewController alloc]init];
    
//    Snapshot_VC.VCType = 0;
//    Loveshot_VC.VCType = 0;
//    GoodArticle_VC.VCType = 0;
    
//    GoodFriend_VC.view.backgroundColor = [UIColor cyanColor];
//    Loveshot_VC.view.backgroundColor = [UIColor purpleColor];
//    GoodArticle_VC.view.backgroundColor = [UIColor yellowColor];
//    GoodArticle_VC0.view.backgroundColor = [UIColor greenColor];
    
    [arr addObject:GoodFriend_VC];
    [arr addObject:Loveshot_VC];
    [arr addObject:GoodArticle_VC];
    [arr addObject:GoodArticle_VC0];
    
    return arr;
}
#pragma mark -懒加载

-(UIButton *)addBtn{
    if (!_addBtn) {
        _addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _addBtn.frame = CGRectMake(0, 0, 44, 44);
        [_addBtn setImage:[UIImage imageNamed:@"addBtnImg"] forState:UIControlStateNormal];
        [_addBtn addTarget:self action:@selector(addBtnACTION:) forControlEvents:UIControlEventTouchUpInside];
        _addBtn.tag = 1001;
    }
    return _addBtn;
}
- (UIButton *)fangdajingBtn{
    if (!_fangdajingBtn) {
        _fangdajingBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _fangdajingBtn.frame = CGRectMake(0, 0, 44, 44);
        [_fangdajingBtn setImage:[UIImage imageNamed:@"jw_home_fangdajin"] forState:UIControlStateNormal];
        [_fangdajingBtn addTarget:self action:@selector(addBtnACTION:) forControlEvents:UIControlEventTouchUpInside];
        _fangdajingBtn.tag = 1000;
    }
    return _fangdajingBtn;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


//扫描
-(void)storeBtnACTION:(UIButton *)sender{
//    [self code2ACTION];
}
//消息
-(void)addBtnACTION:(UIButton *)sender{

    if (sender.tag == 1000) {
        
        
    }else if (sender.tag == 1001){
        
        AddFriendsVC *vc = [AddFriendsVC new];
        
        [self.navigationController pushViewController:vc animated:YES];
    }
}

- (void)loginHuanxin{
    
    [[EMClient sharedClient] loginWithUsername:k_user_phone
                                      password:kEMPassword
                                    completion:^(NSString *aUsername, EMError *aError) {
                                        if (!aError) {
                                            NSLog(@"登录成功");
                                            
                                        } else {
                                            NSLog(@"登录失败");
                                        }
                                    }];
}

- (void)updataJSONRequest{
    
    [self showBottomMessag:@"啦啦啦啦啦啦啦"];
    
    [self initializeLocationService];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    
    //保存经纬度
    NSString *lat = [NSString stringWithFormat:@"%f",newLocation.coordinate.latitude];
    NSString *lng = [NSString stringWithFormat:@"%f",newLocation.coordinate.longitude];
    [kUserDefaults setObject:lat forKey:@"user_lat"];
    [kUserDefaults setObject:lng forKey:@"user_lng"];
    [kUserDefaults synchronize];
    
     NSLog(@"-=-=>>%@-=-=-111=>>%@",lat,lng);
    // 获取当前所在的城市名
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    //根据经纬度反向地理编译出地址信息
    [geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *array, NSError *error){
        if (array.count > 0){
            CLPlacemark *placemark = [array objectAtIndex:0];
            //获取城市
            UILabel *promptLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 200 , 35)];
            promptLabel.font = [UIFont systemFontOfSize:15];
            NSArray *str =[placemark.addressDictionary objectForKey:@"FormattedAddressLines"];
            promptLabel.text = [str firstObject];
            promptLabel.textColor = [UIColor whiteColor];
            NSDictionary *attrbute = @{NSFontAttributeName:[UIFont systemFontOfSize:15]};
            CGSize siz= [[str firstObject] boundingRectWithSize:CGSizeMake(MAXFLOAT, 35) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attrbute context:nil].size;
            promptLabel.width = siz.width + 20;
            
        }
        else if (error == nil && [array count] == 0)
        {
            NSLog(@"No results were returned.");
        }
        else if (error != nil)
        {
            NSLog(@"An error occurred = %@", error);
        }
    }];
    [manager stopUpdatingLocation];
}

@end
